package com.eastfair.projectcore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.cache.common.AreaCacheKeyBuilder;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.core.utils.StringUtils;
import com.eastfair.projectcore.dao.AreaMapper;
import com.eastfair.projectcore.entity.Area;
import com.eastfair.projectcore.service.AreaService;
import com.eastfair.projectcore.vo.AreaVO;
import com.eastfair.uid.baidu.rule.SnowFlakeRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 业务实现类
 *
 * </p>
 *
 * @author zjn
 * @date 2021-08-23
 */
@Slf4j
@Service
public class AreaServiceImpl extends SuperServiceImpl<AreaMapper, Area> implements AreaService {

    //    @Autowired
//    SelfUidService selfUidService;
    @Autowired
    SnowFlakeRole snowFlakeRole;

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    protected R<Boolean> handlerSave(Area model) {
        model.setId(snowFlakeRole.findUidSimple());
        //雪花ID,不需要分库分表
//        SelfUidDto selfUidDto = new SelfUidDto();
//        model.setId(selfUidService.getUid(selfUidDto));
        return R.successDef();
    }

    @Override
    public String getPhoneCodeByAreaId(Integer id) {
        Area area = getById(id);
        String phoneCode = "";
        if (!Objects.isNull(area)) {
            phoneCode = area.getPhoneCode();
            if (!StringUtils.isBlank(phoneCode)) {
                while (phoneCode.startsWith("0")) {
                    phoneCode = phoneCode.substring(1);
                }
            }
        }
        return phoneCode;
    }

    @Override
    public R<List<AreaVO>> getByLevel(String level) {
        if (StringUtils.isBlank(level)) {
            return R.fail(ExceptionCode.ERROR_NULL_PARAMS);
        }

        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Area::getLevel, level);
        return R.success(ConvertUtil.convertList(list(queryWrapper), AreaVO.class));
    }

    @Override
    public R<List<AreaVO>> getByParent(Long parentId) {
        if (Objects.isNull(parentId)) {
            return R.fail(ExceptionCode.ERROR_NULL_ID);
        }
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Area::getParentId, parentId);
        return R.success(ConvertUtil.convertList(list(queryWrapper), AreaVO.class));
    }

    @Override
    public List<AreaVO> queryList(Area area) {
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(area.getLevel())) {
            queryWrapper.lambda().eq(Area::getLevel, area.getLevel());
        }
        if (!StringUtils.isBlank(area.getName())) {
            queryWrapper.lambda().eq(Area::getName, area.getName());
        }
        if (!StringUtils.isBlank(area.getCode())) {
            queryWrapper.lambda().eq(Area::getCode, area.getCode());
        }
        return ConvertUtil.convertList(list(queryWrapper), AreaVO.class);
    }
}
